﻿@using KL.Store.Core;
@using CaptchaMvc.HtmlHelpers;
@{
    Layout = null;
}
<div id="EditGroups">
    @{Html.RenderAction("EditGroupsGet", "ProductManage", new { id = ViewBag.ProductId });}
</div>

<script>

    $('#live_productsearch_results li a').live('click', function ()
    {
        var selectedId = $(this).data('id');
        var selectedName = $(this).html();
        var alreadyAdded = false;
        $('form#AddCategoriesToProduct #productsList ul li span').each(function ()
        {
            if ($(this).html() == selectedName)
            {
                alreadyAdded = true;
            }
        });
        if (!alreadyAdded)
        {
            $('#groupsList ul').append('<li id="group' + selectedId + '"><i class="fa fa-remove" data-delete="' + selectedId + '"></i>&nbsp;<span>' + selectedName + '</span><input type="hidden" name="Groups" value="' + selectedId + '"></li>');
        }
        $('#searchProduct').attr('value', '');
        $('#divProductSearchResult').html('');
        return false;
    });

    $('form#AddCategoriesToProduct #groupsList ul li i.fa-remove').live('click', function ()
    {
        var toRemove = $(this).data('delete');
        $('form#AddCategoriesToProduct #groupsList ul li#group' + toRemove).remove();
        return false;
    });

    function grupSecilmemis()
    {
        $('#formResult').html('<p class="bg-danger">Grup Seçilmemiş.</p>');
    }
    function urunSecilmemis()
    {
        $('#formResult').html('<p class="bg-danger">Ürün Seçilmemiş.</p>');
    }
    function gruplarEklendi(sonucMetni)
    {
        $('#EditGroups').load('/ProductManage/EditGroupsGet', { id: '@(ViewBag.ProductId)' }, function ()
        {
            $('#formResult').html('<p class="bg-success">' + sonucMetni + '</p>');
            $('#productsList ul').html('');
        });
    }

    $('.editGroupList ul.editGroupListUl li i.fa-remove').live('click', function ()
    {
        if (confirm('Silmek istediğinizden emin misiniz?'))
        {
            var toRemoveProduct = $(this).data('product');
            var toRemoveGroup = $(this).data('group');
            $('#piResult').load('/ProductManage/RemoveCategoryFromProduct', { ProductId: toRemoveProduct, CategoryId: toRemoveGroup }, function ()
            {
                $('#EditGroups').load('/ProductManage/EditGroupsGet', { id: '@(ViewBag.ProductId)' });
            });
        }
    });
</script>
